package com.welike.shibo.controller.v2.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welike.shibo.aspect.annotation.AutoLog;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.exception.BaseException;
import com.welike.shibo.service.VideoService;
import com.welike.shibo.utils.CommonUtil;
import com.welike.shibo.utils.ResultUtils;
import com.welike.shibo.utils.Tools;
import com.welike.shibo.vo.VideoInfoListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019/11/20 5:30 下午
 */

@Slf4j
@Api(value = "视频相关接口")
@RestController
@RequestMapping("/v2/api/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @AutoLog(operateState = "视频列表")
    @RequiresPermissions("video:list")
    @GetMapping(value = "/list")
    public JsonResult list(
            @ApiParam(name = "page", value = "页数", required = false)
            @RequestParam(value = "page", defaultValue = "1", required = false) int page,
            @ApiParam(name = "pageSize", value = "每页条数", required = false)
            @RequestParam(value = "pageSize", defaultValue = "10", required = false)int pageSize
    ){
        log.info("getVideoList");
        Page<VideoInfoListVO> p = new Page<>(page, pageSize);
        IPage<VideoInfoListVO> iPage = videoService.selectVideoPageVo(p);
        Map<String, Object> result = new HashMap<>();
        result.put("list", iPage);
        return ResultUtils.success(result);
    }

    @AutoLog(operateState = "上传视频")
    @RequiresPermissions("video:add")
    @PostMapping(value = "/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile video) throws IOException {
        String name = Tools.getYearMonth() + System.currentTimeMillis();
        if (!videoService.screenshot(video, name)){
            return ResultUtils.error("上传失败");
        }
        Map<String, Object> result = new HashMap<>(1);
        result.put("img", name + ".jpg");
        result.put("video", name + ".mp4");
        return ResultUtils.success(result);
    }

    @AutoLog(operateState = "添加视频")
    @RequiresPermissions("video:add")
    @PostMapping(value = "/create")
    public JsonResult create(@RequestBody JSONObject requestJson, HttpServletRequest request) throws UnknownHostException {
        return videoService.create(requestJson, request);
    }

    @AutoLog(operateState = "视频更新")
    @RequiresPermissions("video:update")
    @DeleteMapping(value = "/delete")
    public JsonResult delete(
            @RequestBody JSONObject requestJson
    ){
        CommonUtil.hasAllRequired(requestJson, "id");
        String id = requestJson.getString("id");
        if (null == id) {
            return ResultUtils.error("删除失败!");
        }
        return videoService.removeVideo(id);
    }

    @AutoLog(operateState = "更新视频状态")
    @RequiresPermissions("video:update")
    @PostMapping(value = "/status")
    public JsonResult changeVideoStatus(
            HttpServletRequest request,
            @RequestBody JSONObject requestJson
    ){
        log.info("changeVideoStatus");
        try {
            CommonUtil.hasAllRequired(requestJson, "id, status");
            String id = requestJson.getString("id");
            Integer status = requestJson.getIntValue("status");
            if (null == id) {
                return ResultUtils.error("审核失败!");
            }
            videoService.updateVideoStatus(id, status);
        } catch (BaseException e){
            log.info(e.getMessage());
            return ResultUtils.error(e.getMessage());
        }
        return ResultUtils.success();
    }
}
